package com.xyq.fs.views;

import java.awt.Desktop;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.document.Document;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import com.xyq.fs.base.MyDirectoryS;
import com.xyq.fs.entity.MyFile;

import com.xyq.fs.search.MySearch;
import com.xyq.fs.search.SearchBody;

import com.xyq.fs.views.UIutil.MyData;
import com.xyq.fs.views.WindowsGo;

import javafx.application.Application;
import javafx.application.Platform;

import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;

import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.VBox;

import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

/**
 * 查询结果窗口
 * 
 * @author xyq
 *
 */
public class WindowsB extends Application {

	
	static SimpleDateFormat sdf = UIutil.sdf;
	static FileChooser fileChooser = UIutil.fileChooser;
	static Desktop desk = UIutil.desk;

	
	private TableView<MyData> tableView = WindowsGo.uu.tableView;

	private Button btnc = WindowsGo.uu.BtnC;
	// 下一页
	private Button btnNext = WindowsGo.uu.btnNext;
	private Button btnPre = WindowsGo.uu.btnPre;


	/**
	 * 构建一个查询参数对象
	 */
	private SearchBody body;

	// 最大页数
	private int maxPageNum = 0;

	// 分页标记
	private Map<Integer, ScoreDoc> fenyeMap = new HashMap<>();

	// 全局缓存对象
	public static Map<String, MyFile> cacheMyFile = UIutil.cacheMyFile;

	/**
	 * 构建查询对象
	 */
	private ObservableList<MyData> dataa = WindowsGo.uu.dataa;

	// 绑定文件名
	TableColumn<MyData, TextFlow> nameCol = WindowsGo.uu.nameCol;
	// 绑定文件预览按钮
	TableColumn<MyData, Button> seeCol = WindowsGo.uu.seeCol;
	// 绑定访问目录按钮
	TableColumn<MyData, Button> dirCol = WindowsGo.uu.dirCol;
	// 绑定复制文件按钮
	TableColumn<MyData, Button> cpoyCol = WindowsGo.uu.cpoyCol;


	Group root = WindowsGo.uu.root;
	Scene scene = WindowsGo.uu.scene;
	VBox vBox = WindowsGo.uu.vBox;
	
	

	private Map<Integer, MyData> myDataMap = WindowsGo.uu.myDataMap;

	private int pageNum = 1;
	
	

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	private Query query;

	private int totalPageNum = 0;

	public WindowsB(SearchBody body, Query query) {

		this.body = body;
		this.query = query;
		
		
	}

	private Stage primaryStage;
	private Text t1 = WindowsGo.uu.t1;
	@Override
	public void start(Stage primaryStage) throws Exception {

		this.primaryStage = primaryStage;
		/**
		 * 第一次查询
		 */
		Platform.runLater(() -> {

			String sb = body.getCachePre() + pageNum;
			MyFile my = cacheMyFile.get(sb);
			if (my == null || WindowsGo.SLEEP_NOW == 1) {
				my = MySearch.search(body.isHasFirst(), query, fenyeMap.get(pageNum));
				// 加入缓存
				cacheMyFile.put(sb, my);
			}
			Document doc;
			for (int i = 0; i < my.getDocList().size(); i++) {
				doc = my.getDocList().get(i);
				MyData md = WindowsGo.uu.getMyData(doc);
				dataa.add(md);
				myDataMap.put(i, md);
			}

			tableView.setVisible(true);

			fenyeMap.put(pageNum + 1, my.getAfterScoreDoc());
			
			maxPageNum = my.getMaxPageNum();
			my.getTotalResultNum();
			body.setHasFirst(true);

			totalPageNum = my.getMaxPageNum() == 1 ? my.getMaxPageNum() : my.getMaxPageNum();
			t1.setText("关键词:  \"" + body.getSearchKey() + "\"  查询到了" + (my.getTotalResultNum()) + "条相关记录");
			btnc.setText("当前页: " + pageNum + "/" + totalPageNum);
			getNextPage(1);
		});
		if (MyDirectoryS.writer == null) {
			primaryStage.close();
			MyAlert.alert("工具初始化中,请稍后..");
			return;
		}
		primaryStage.setScene(scene);

		// 在scene中修改光标

		/**
		 * 点击上一页按钮
		 */
		EventHandler<ActionEvent> btnPrePage = new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent t) {

				Platform.runLater(() -> {
				
					if (pageNum > 1) {
						pageNum--;
						String sb = body.getCachePre() + pageNum;

						MyFile my = cacheMyFile.get(sb);
						if (my == null) {
							my = MySearch.search(body.isHasFirst(), query, fenyeMap.get(pageNum));
						}
						for (int i = 0; i < my.getDocList().size(); i++) {
							Document doc = my.getDocList().get(i);
							MyData md = myDataMap.get(i);
							myDataMap.put(i, md);
							dataa.set(i, md);
							md.setDoc(doc);
							
						}
						btnc.setText("当前页: " + pageNum + "/" + totalPageNum);
					} else {
						MyAlert.alertError("已经是第一页了!!!");
					}
				});
			}
		};

		btnPre.setOnAction(btnPrePage);
		/**
		 * 点击下一页按钮
		 */
		EventHandler<ActionEvent> btnNextPage = new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent t) {

				//new Thread(() -> {
					
					if (pageNum < maxPageNum) {
						pageNum++;
						String sb = body.getCachePre() + pageNum;
						MyFile my = cacheMyFile.get(sb);
						if (my == null) {
							my = MySearch.search(body.isHasFirst(), query, fenyeMap.get(pageNum));
							cacheMyFile.put(sb, my);
						}
						for (int i = 0; i < my.getDocList().size(); i++) {
							Document doc = my.getDocList().get(i);
							MyData md = myDataMap.get(i);
							dataa.set(i, md);
							md.setDoc(doc);
						}
						// 加入缓存
						fenyeMap.put(pageNum + 1, my.getAfterScoreDoc());
						 getNextPage(1);
						Platform.runLater(() -> {
							btnc.setText("当前页: " + pageNum + "/" + totalPageNum);
						});
						sb = null;
					} else {
						MyAlert.alertError("已经是最后一页了!!!");
					}
				//}).start();

			}
		};
		btnNext.setOnAction(btnNextPage);

	}

	public void show() {

		primaryStage.show();

	}

	 private void getNextPage(int n) {
	
	 new Thread(() -> {
	
	 int next = pageNum + n;
	 String sb = body.getCachePre() + (next);
	 MyFile my2 = cacheMyFile.get(sb);
	 if (my2 == null || WindowsGo.SLEEP_NOW == 0) {
	 // 缓存下一页
	 MyFile my3 = MySearch.search(body.isHasFirst(), query,
	 fenyeMap.get(next));
	 cacheMyFile.put(sb, my3);
	 fenyeMap.put(next + 1, my3.getAfterScoreDoc());
	 }
	
	 }).start();
	
	 }
}
